package benny.ApkposStore.Activity;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import benny.ApkposStore.R;
import benny.ApkposStore.utils.ApkConstant;
import benny.ApkposStore.utils.ApkUntils;
import benny.ApkposStore.utils.Apkglobal;
import benny.ApkposStore.utils.NetUtils;

public class LoadingActivity extends Activity {
	private Timer playImagetimer = null;
	private int timeint = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		ApkUntils.fullscreen(this);
		setContentView(R.layout.loading);

		// 初始化网络工具类
		NetUtils.init(getApplicationContext());
		NetUtils.setTimeOut(ApkConstant.HTTP_CONNECT_TIMEOUT,
				ApkConstant.HTTP_READ_TIMEOUT);
		Apkglobal.setuuid(ApkUntils.getMyUUID(this));
		playImagetimer = new Timer("playImage");
		//playImagetimer.scheduleAtFixedRate(palyimgtask, 0, 100);
		playImagetimer.schedule(palyimgtask, 0, 100);
	}

	private void cleanSendTimerTask() {
		if (palyimgtask != null) {
			palyimgtask.cancel();
			palyimgtask = null;
		}
		if (playImagetimer != null) {
			playImagetimer.cancel();
			playImagetimer.purge();
			playImagetimer = null;
		}
	}

	private TimerTask palyimgtask = new TimerTask() {
		

		@Override
		public void run() {
			if (timeint > 20) {
				String CDKEY = ApkUntils.ReadSharedPreferences(
						LoadingActivity.this, "CDKEY", "cdkey");
				System.out.println(CDKEY);
				if (CDKEY.length() > 0) {
					Apkglobal.setcdkey(CDKEY);
					Intent intent = new Intent();
					intent.setClass(LoadingActivity.this,
							ApkposStoreActivity.class);
					startActivity(intent);
					cleanSendTimerTask();
					// ApkUntils.openActivityAnim(RegisterActivity.this,10);
					finish();
				} else {

					Intent intent = new Intent();
					intent.setClass(LoadingActivity.this,
							RegisterActivity.class);
					startActivity(intent);
					cleanSendTimerTask();
					// ApkUntils.openActivityAnim(RegisterActivity.this,10);
					finish();
					
				}
				return;
			}
			
			timeint++;
		}
	};

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		System.gc(); // 回收系统资源
		super.onDestroy();
	}

}
